Attribute VB_Name = "modBatch"
'---------------------------------------------------------------------------------------
'Module : modBatch
'Date   : 03/17/2004
'Author : Chris Elwell
'Purpose: Contains the routines required for batch processing.
'         ProcessBatch(ByVal strFile As String) - main routine for processing batch.
'               Uses info on the Batch Input tab to create FXRS transaction.
'
'         WriteBatchReply() - Uses the info on the Batch Output tab in Setup to create
'                             a BatchReply file for the customer.
'         WriteBatchError() - Writes out a file including the error text and the original
'                             data.
'         BatchErrorEdit() - This is called when an item is double clicked in the
'                           Error Transaction (lstErrorTransactions) list box. CIC
'                           can code to map batch fields to screens fields for editing.
'
'2007-04-17, JFW, Put label early in the trans to allow batch default or input to override it
'2007-05-15, JFW, TD MPS with FXRS 840.  New boolean TDChild for logic flow.
'
'---------------------------------------------------------------------------------------
Option Explicit

Dim m_strLine As String         'Current line read from batch file
'---------------------------------------------------------------------------------------
' Procedure : ProcessBatch(ByVal strFile As String)
' DateTime  : 03/17/2004
' Purpose   : Sets strFile as the input file. Reads a line at a time and assigns the
'             fields in the line to the correct global var. Then calls routines to build
'             and send data. Processing continues when g_blnComplete is set to true in
'             frmShip.g_clsParser_ParseComplete().
' Call from : frmShip.cmdShipAll_Click()
'---------------------------------------------------------------------------------------
'
Public Sub ProcessBatch(ByVal strFile As String)
    Dim strFields() As String
    Dim FSO As New Scripting.FileSystemObject
    Dim fsoFileIn As Scripting.TextStream
    Dim i, j As Integer
    Dim strTrans As String
    Dim strDelimIn As String
    Dim intUboundInputFlds As Integer
    Dim ret
    Dim strMultiFields() As String
    
    On Error GoTo EH
    g_strErrorLocation = "modBatch:ProcessBatch"
    
    Set fsoFileIn = FSO.OpenTextFile(strFile, 1)
    
    g_intErrorCount = 0
    g_intShipCount = 0
    
    'CIC defineable routine. See end of this module.
    Call BeforeBatch
    
    Do While Not fsoFileIn.AtEndOfStream
            
        If g_blnAbortBatch Then
            ret = MsgBox("Click OK to resume processing, click Cancel to abort processing.", vbOKCancel, "Batch Processing Paused")
    
            If ret = vbOK Then
                g_blnAbortBatch = False
            Else
                Exit Do
            End If
        End If
        
        'Set the delimiter in the input transaction
        With frmSetup
            'Get total fields configured on Batch Input tab
            For i = 0 To .txtInputFld.UBound
                If .txtInputFld(i) <> "" Then
                    intUboundInputFlds = i
                End If
            Next
            Select Case .cboDelimIn
                Case "Comma"
                    strDelimIn = ","
                Case "Tab"
                    strDelimIn = vbTab
                Case "Pipe"
                    strDelimIn = "|"
                Case "Tilde"
                    strDelimIn = "`"
            End Select
            'Split the inout into an array using the delimiter from above
            g_blnComplete = False
        
            m_strLine = fsoFileIn.ReadLine
            strFields = Split(m_strLine, strDelimIn)
            'If the Ubound of the array doesn't match the number of defined fields or 1 for deletes
            'then send an error with the data received and exit
            If UBound(strFields) <> intUboundInputFlds And UBound(strFields) <> 0 Then
                MsgBox "Number of input fields does not match number of configured input fields." _
                                   & " Data Received: " & m_strLine
                Exit Sub
            End If
            
            strTrans = ""
            'Deletes
            If UBound(strFields) = 0 Then
               TransactionBuilder strTrans, 0, "023"                        'Transaction type
               TransactionBuilder strTrans, 1, "BatchDelete"                'Transaction ID
               TransactionBuilder strTrans, 29, Trim(strFields(0))          'Trk#
               TransactionBuilder strTrans, 498, Trim(.f498)                'Meter Number
            'Shipping
            Else
                'Add first fields
                TransactionBuilder strTrans, 0, "020"
                
                ' 2007-05-08, JFW, Default account and meter.
                TransactionBuilder strTrans, 10, g_str10                            'Acct Number
                TransactionBuilder strTrans, 498, g_str498                          'Meter
                
                ' 2007-05-08, JFW, Put defaults here, before input
                '2007-04-17, JFW, Put label early in the trans to allow batch default or input to override it
                'Added 11/16 to add label flds to batch
                If g_blnUseLabelFldsInBatch Then
                    TransactionBuilder strTrans, 187, g_str187                          'Label Type
                    TransactionBuilder strTrans, 537, g_str537                          'Thermal/Laser Label prt UNC/File Path
                    If g_str187 <> "DIB" And g_str187 <> "PNG" Then
                        TransactionBuilder strTrans, 1282, g_str1282                        'Printer Type Indicator
                    End If
                    TransactionBuilder strTrans, 1660, g_str1660                        'Custom label flag
                End If
        
                'Add fields with default values
                For i = 0 To .txtFieldNum.UBound
                    'Format weight field for 2 implied decimals
                    If .txtFieldNum(i) = "1670" Then
                        TransactionBuilder strTrans, .txtFieldNum(i), RemoveSpecial(ImpliedDecimal(.txtValue(i)))
                    ElseIf .txtFieldNum(i) = "541" Then
                        g_blnTDChild = True
                        TransactionBuilder strTrans, 541, "NNNYNNNNN"                               'Open Ship Flags
                    ElseIf .txtFieldNum(i) = "542" Then
                        g_blnTDChild = True
                        TransactionBuilder strTrans, 542, g_strTDOpenShip
                    ElseIf .txtFieldNum(i) <> "" Then
                        TransactionBuilder strTrans, .txtFieldNum(i), .txtValue(i)
                    End If
                    '******************************************************************************
                    'Added to save batch to local database. Remove when History comes from Sybase
                    Select Case .txtFieldNum(i)
                        Case "25"
                            g_str25 = .txtValue(i)
                        Case "3056"
                            g_str3056 = .txtValue(i)
                        Case "3057"
                            g_str3057 = .txtValue(i)
                        Case "50"
                            g_str50 = .txtValue(i)
                        Case "10"
                            g_str10 = .txtValue(i)
                        Case "1274"
                            g_str1274 = .txtValue(i)
                        Case "24"
                            g_str24 = .txtValue(i)
                        Case "116"
                            g_str116 = .txtValue(i)
                    End Select
                    '******************************************************************************
                Next
                
                ' 2007-05-08, JFW, Batch input data here, after defaults above.
                'Add fields sent from customer
                For i = 0 To UBound(strFields)
                    If Trim(.txtInputFld(i)) <> "N/A" Then
                        'Added 12/3 to support custom fields (g_strCUST(0) -> g_strCUST(9))
                        'in batch for custom labels (non-FXRS Fields)
                        If Left(.txtInputFld(i), 4) = "CUST" And InStr(.txtInputFld(i), "&") = 0 Then
                            g_strCUST(Val(Right(.txtInputFld(i), 1))) = strFields(i)
                        End If
                        'Remove special characters from ZIP code fields and Sender and Recip phone # fields
                        If .txtInputFld(i) = "17" Or .txtInputFld(i) = "9" Or .txtInputFld(i) = "18" _
                            Or .txtInputFld(i) = "183" Then
                            
                            strFields(i) = RemoveSpecial(strFields(i))
                        
                        End If
                        'Format these fields for 2 implied decimals
                        'Weight, COD Amount, CI Chgs, Dry Ice Wgt
                        If Left(.txtInputFld(i), 4) = "1670" Or .txtInputFld(i) = "53" Or .txtInputFld(i) = "532" _
                            Or .txtInputFld(i) = "533" Or .txtInputFld(i) = "534" Or .txtInputFld(i) = "1684" Then
                            
                            strFields(i) = RemoveSpecial(ImpliedDecimal(strFields(i)))
                        
                        End If
                        'Format Commodity Unit Value for 6 implied decimals, account for multiple occurrence fields
                        If Left(.txtInputFld(i), 4) = "1030" Then
                            strFields(i) = RemoveSpecial(ImpliedDecimal(strFields(i), 6))
                        End If
                        'Format Alcohol Volume for 3 implied decimals
                        If .txtInputFld(i) = "52" Then
                            strFields(i) = RemoveSpecial(ImpliedDecimal(strFields(i), 3))
                        End If
                        'Convert Service Codes if configured in setup
                        If .txtInputFld(i) = "1274" And frmSetup.chkConvertSvcCodes = 1 Then
                            If .cboQualIn = "None" Then
                                strFields(i) = ConvertServiceType(strFields(i))
                            Else
                                strFields(i) = ConvertServiceType(Replace(strFields(i), .cboQualIn, ""))
                            End If
                        End If
                        'Added to allow more than one FXRS field to be populated per position
                        'in the batch file. E.G. One position can populate both transID and references.
                        If InStr(.txtInputFld(i), "&") > 0 Then
                            strMultiFields = Split(.txtInputFld(i), "&")
                            For j = 0 To UBound(strMultiFields)
                                'Added 12/3 to support Custom Label Fields
                                If Left(strMultiFields(j), 4) = "CUST" Then
                                    g_strCUST(Val(Right(strMultiFields(j), 1))) = strFields(i)
                                ElseIf .cboQualIn = "None" Then
                                    TransactionBuilder strTrans, Trim(strMultiFields(j)), strFields(i)
                                Else
                                    TransactionBuilder strTrans, Trim(strMultiFields(j)), Replace(strFields(i), .cboQualIn, "")
                                End If
                                '******************************************************************************
                                'Added to save batch to local database. Remove when History comes from Sybase
                                Select Case strMultiFields(j)
                                    Case "18"
                                        If .cboQualIn = "None" Then
                                            g_str18 = strFields(i)
                                        Else
                                            g_str18 = Replace(strFields(i), .cboQualIn, "")
                                        End If
                                    Case "1670"
                                        If .cboQualIn = "None" Then
                                            g_str1670 = strFields(i)
                                        Else
                                            g_str1670 = Replace(strFields(i), .cboQualIn, "")
                                        End If
                                    Case "25"
                                        If .cboQualIn = "None" Then
                                            g_str25 = strFields(i)
                                        Else
                                            g_str25 = Replace(strFields(i), .cboQualIn, "")
                                        End If
                                    Case "3056"
                                        If .cboQualIn = "None" Then
                                            g_str3056 = strFields(i)
                                        Else
                                            g_str3056 = Replace(strFields(i), .cboQualIn, "")
                                        End If
                                    Case "3057"
                                        If .cboQualIn = "None" Then
                                            g_str3057 = strFields(i)
                                        Else
                                            g_str3057 = Replace(strFields(i), .cboQualIn, "")
                                        End If
                                End Select
                                '******************************************************************************
                            Next
                        Else
                            If .cboQualIn = "None" Then
                                TransactionBuilder strTrans, Trim(.txtInputFld(i)), strFields(i)
                            Else
                                TransactionBuilder strTrans, Trim(.txtInputFld(i)), Replace(strFields(i), .cboQualIn, "")
                            End If
                        End If
                        '******************************************************************************
                        'Added to save batch to local database. Remove when History comes from Sybase
                        Select Case .txtInputFld(i)
                            Case "18"
                                If .cboQualIn = "None" Then
                                    g_str18 = strFields(i)
                                Else
                                    g_str18 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "1670"
                                If .cboQualIn = "None" Then
                                    g_str1670 = strFields(i)
                                Else
                                    g_str1670 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "25"
                                If .cboQualIn = "None" Then
                                    g_str25 = strFields(i)
                                Else
                                    g_str25 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "3056"
                                If .cboQualIn = "None" Then
                                    g_str3056 = strFields(i)
                                Else
                                    g_str3056 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "3057"
                                If .cboQualIn = "None" Then
                                    g_str3057 = strFields(i)
                                Else
                                    g_str3057 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "11"
                                If .cboQualIn = "None" Then
                                    g_str11 = strFields(i)
                                Else
                                    g_str11 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "12"
                                If .cboQualIn = "None" Then
                                    g_str12 = strFields(i)
                                Else
                                    g_str12 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "13"
                                If .cboQualIn = "None" Then
                                    g_str13 = strFields(i)
                                Else
                                    g_str13 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "15"
                                If .cboQualIn = "None" Then
                                    g_str15 = strFields(i)
                                Else
                                    g_str15 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "16"
                                If .cboQualIn = "None" Then
                                    g_str16 = strFields(i)
                                Else
                                    g_str16 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "17"
                                If .cboQualIn = "None" Then
                                    g_str17 = strFields(i)
                                Else
                                    g_str17 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "50"
                                If .cboQualIn = "None" Then
                                    g_str50 = strFields(i)
                                Else
                                    g_str50 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "10"
                                If .cboQualIn = "None" Then
                                    g_str10 = strFields(i)
                                Else
                                    g_str10 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "1274"
                                If .cboQualIn = "None" Then
                                    g_str1274 = strFields(i)
                                Else
                                    g_str1274 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "24"
                                If .cboQualIn = "None" Then
                                    g_str24 = strFields(i)
                                Else
                                    g_str24 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "116"
                                If .cboQualIn = "None" Then
                                    g_str116 = strFields(i)
                                Else
                                    g_str116 = Replace(strFields(i), .cboQualIn, "")
                                End If
                            Case "541"
                                g_blnTDChild = True
                            Case "542"
                                g_blnTDChild = True
                        End Select
                        '******************************************************************************
                    End If
                Next
            
            End If
        End With
        '******************************************************************************
        'Added to save batch to local database. Remove when History comes from Sybase
        If g_str116 = "" Then g_str116 = 1
        If g_str24 = "" Then g_str24 = Format(Date, "YYYYMMDD")
        '******************************************************************************
        'Added 7/30 to support batch error checking
        If frmShip.chkBatchErrorCheck = 1 Then
            TransactionBuilder strTrans, 184, "Y"
        End If
        
        'Make up for FXRS0725
        If 0 = InStr(1, strTrans, """74,""") Then
            TransactionBuilder strTrans, 74, g_str50
        End If
        
        'Add 99, ""
        TransactionBuilder strTrans, 99, ""
         
        'CIC defineable routine. See end of this module.
        Call BeforeBatchShip
        
        Call SendData(strTrans)
        
        Do While Not g_blnComplete
            DoEvents
            Call Pause(0.1)
        Loop
    Loop
    
    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub
'---------------------------------------------------------------------------------------
' Procedure : WriteBatchReply()
' DateTime  : 2/18/2004
' Purpose   : Use this routine to handle FXRS reply data
' Call from : frmShip.g_clsParser_ParseComplete()
'---------------------------------------------------------------------------------------
'
Public Sub WriteBatchReply()
    Dim strPath As String
    Dim strLineOut As String
    Dim strDelimOut As String
    Dim strQualOut As String
    Dim intUboundOutputFlds As Integer
    Dim i As Integer
    
    On Error GoTo EH
    g_strErrorLocation = "modBatch:WriteBatchReply"
    
    With frmSetup
        'Get the number of output fields
        For i = 0 To .txtOutputFld.UBound
            If .txtOutputFld(i) <> "" Then intUboundOutputFlds = i
        Next
        'Get the output delimiter
        Select Case .cboDelimOut
            Case "Comma"
                strDelimOut = ","
            Case "Tab"
                strDelimOut = vbTab
            Case "Pipe"
                strDelimOut = "|"
            Case "Tilde"
                strDelimOut = "`"
        End Select
        'Get output text qualifier
        strQualOut = IIf(.cboQualOut = "None", "", .cboQualOut)
        
        'Set path to program path or File Path defined is Setup Batch Defaults tab
        strPath = IIf(.txtFilesPath = "", App.Path, .txtFilesPath)
        
        'Add Input Fields to Output file if configured
        If .chkAddInputToOutput = 1 Then strLineOut = m_strLine & ","
        
    End With
    
    With frmSetup
        For i = 0 To intUboundOutputFlds
            'Added reference fields since these fields aren't returned in the reply transaction
            If .txtOutputFld(i) = "25" Then
                strLineOut = strLineOut & strQualOut & g_str25 & strQualOut & strDelimOut
            ElseIf .txtOutputFld(i) = "3056" Then
                strLineOut = strLineOut & strQualOut & g_str3056 & strQualOut & strDelimOut
            ElseIf .txtOutputFld(i) = "3057" Then
                strLineOut = strLineOut & strQualOut & g_str3057 & strQualOut & strDelimOut
            Else
                strLineOut = strLineOut & strQualOut & TransactionParser(g_strSaveReply, .txtOutputFld(i)) & strQualOut & strDelimOut
            End If
        Next
    End With
    
    'Remove last delimiter
    strLineOut = Left(strLineOut, Len(strLineOut) - 1)
    
    'Increment shipped counter
    g_intShipCount = g_intShipCount + 1
    
    'Update Batch tab with totals
    With frmShip
        .txtNumShipped = Str(g_intShipCount)
        .txtTotalRecords = Str(g_intShipCount + g_intErrorCount)
    End With

    'Write reply fields to output file
    Open strPath & "\BatchReply_" & Format(Date, "YYYYMMDD") & ".txt" For Append As #1
    Print #1, strLineOut
    Close #1
    
    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub
'---------------------------------------------------------------------------------------
' Procedure : WriteBatchError()
' DateTime  : 2/18/2004
' Purpose   : Use this routine to handle FXRS errors
' Call from : frmShip.g_clsParser_ParseComplete()
'---------------------------------------------------------------------------------------
'
Public Sub WriteBatchError()
    
    Dim strPath As String
    Dim strDelimIn As String
    
    On Error GoTo EH
    g_strErrorLocation = "modBatch:WriteBatchError"
    
    With frmSetup
        'Set the delimiter in the input transaction
        Select Case .cboDelimIn
            Case "Comma"
                strDelimIn = ","
            Case "Tab"
                strDelimIn = vbTab
            Case "Pipe"
                strDelimIn = "|"
            Case "Tilde"
                strDelimIn = "`"
        End Select
        'Set path to program path or File Path defined on Setup Batch Defaults tab
        If .txtFilesPath = "" Then
            strPath = App.Path
        Else
            strPath = .txtFilesPath
        End If
    End With
    
    With frmShip
        'Write error message and transaction text to file
        Open strPath & "\BatchErrors_" & Format(Date, "YYYYMMDD") & ".txt" For Append As #1
        'Added code to handle rating errors when performing Batch Error Check
        If g_str3 <> "" Then
            Print #1, g_str3 & strDelimIn & m_strLine
            g_intErrorCount = g_intErrorCount + 1
        ElseIf g_str1125 <> "0" And g_str1125 <> "" Then
            If g_str560 <> "" Then
                Print #1, g_str560 & strDelimIn & m_strLine
            Else
                Print #1, GetRateError(g_str1125) & strDelimIn & m_strLine
            End If
            g_intErrorCount = g_intErrorCount + 1
        End If
        
        Close #1
        .txtNumErrors = Str(g_intErrorCount)
        .txtTotalRecords = Str(g_intShipCount + g_intErrorCount)
        If Not g_blnBatchErrorCheck Then
            .lstErrorTransactions.AddItem g_str3 & "," & m_strLine
        End If
    End With
    
    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub
'---------------------------------------------------------------------------------------
' Procedure : BatchErrorEdit()
' DateTime  : 4/15/2004
' Modified  : 8/9/2004
'           : Added code to apply service type conversion if checked in setup.
' Purpose   : When a line in Error list on the Batch tab is dbl-clicked, this is called
'             and the data is sent to the Ship screen for editing.
' Call from : frmShip.lstErrorTransactions_DblClick()
'---------------------------------------------------------------------------------------
'
Public Sub BatchErrorEdit(ByVal strTrans As String)
    Dim strFields() As String
    Dim strMultipleFlds() As String     'Used for positions containing "&"
    Dim i, j As Integer
    Dim MyControl As Control
    Dim strDelimIn As String
    
    On Error GoTo EH
    g_strErrorLocation = "modBatch:BatchErrorEdit"
        
    With frmSetup
        'Set the delimiter in the input transaction
        Select Case .cboDelimIn
            Case "Comma"
                strDelimIn = ","
            Case "Tab"
                strDelimIn = vbTab
            Case "Pipe"
                strDelimIn = "|"
            Case "Tilde"
                strDelimIn = "`"
        End Select
        strFields = Split(strTrans, strDelimIn)
        
        'The first field is the error message
        frmShip.StatusBar1.Panels(3) = strFields(0)
        
        'If the control name is the same as defined in setup
        'assign the value in the array to the control
        For i = 0 To .txtInputFld.UBound
            For Each MyControl In frmShip.Controls
                If TypeOf MyControl Is TextBox Or TypeOf MyControl Is ComboBox Then
                    'Added 7/28 to account for positions with "&"
                    If InStr(.txtInputFld(i), "&") > 0 Then
                        strMultipleFlds = Split(.txtInputFld(i), "&")
                        For j = 0 To UBound(strMultipleFlds)
                            If MyControl.name = "f" & strMultipleFlds(j) Then
                                If .cboQualIn = "None" Then
                                    MyControl = strFields(i + 1)
                                Else
                                    MyControl = Replace(strFields(i + 1), .cboQualIn, "")
                                End If
                            End If
                        Next
                    ElseIf MyControl.name = "f" & .txtInputFld(i) Then
                        'Added 8/9/04 to apply service type conversions if configured in setup
                        If MyControl.name = "f1274" And frmSetup.chkConvertSvcCodes = 1 Then
                            If .cboQualIn = "None" Then
                                MyControl = ConvertServiceType(strFields(i + 1))
                            Else
                                MyControl = ConvertServiceType(Replace(strFields(i + 1), .cboQualIn, ""))
                            End If
                        ElseIf .cboQualIn = "None" Then
                            MyControl = strFields(i + 1)
                        Else
                            MyControl = Replace(strFields(i + 1), .cboQualIn, "")
                        End If
                    End If
                End If
            Next
        Next
        
        'If the control name is the same as defined in setup
        'assign the value in the value field
        For i = 0 To .txtFieldNum.UBound
            For Each MyControl In frmShip.Controls
                If TypeOf MyControl Is TextBox Then
                    If MyControl.name = "f" & .txtFieldNum(i) Then
                        MyControl = .txtValue(i)
                    End If
                ElseIf TypeOf MyControl Is ComboBox Then
                    If MyControl.name = "f" & .txtFieldNum(i) And MyControl.Style = 0 Then
                        MyControl = .txtValue(i)
                    End If
                End If
            Next
        Next
    End With
    
    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub
